﻿using Learun.Application.Base.SystemModule;
using Learun.Application.Organization;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2020-12-04 15:20
    /// 描 述：项目管理
    /// </summary>
    public class ec_projectBLL : ec_projectIBLL
    {
        private ec_projectService ec_projectService = new ec_projectService();

        #region 获取数据
        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<ec_projectEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var ProjectId = queryParam["ProjectId"]?.ToString();
                var ProjectName = queryParam["ProjectName"]?.ToString();

                var query = Db.Queryable<ec_projectEntity>()
                    .WhereIF(!string.IsNullOrEmpty(ProjectId), x => x.ProjectId == ProjectId)
                    .WhereIF(!string.IsNullOrEmpty(ProjectName), x => x.ProjectName.Contains(ProjectName));
                List<ec_projectEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                var users = Db.Queryable<UserEntity>().ToList();
                string[] codes = new string[] { "Be_ShipTypeCN", "Be_ShipTypeEN", "Be_ShipSociety", "Be_FlagState" };
                var details = Db.Queryable<DataItemDetailEntity>()
                    .InnerJoin<DataItemEntity>((a, b) => a.F_ItemId == b.F_ItemId && codes.Contains(b.F_ItemCode))
                    .Select((a, b) => new DataItemDetailEntity()
                    {
                        F_ItemValue = a.F_ItemValue,
                        F_ItemName = a.F_ItemName,
                        F_ItemCode = b.F_ItemCode,
                    }).ToList();
                list?.ForEach(x =>
                {
                    x.CreateUserName = users.FirstOrDefault(y => y.F_UserId == x.CreateUserID)?.F_RealName;
                    x.ShipTypeCNName = details.Where(y => y.F_ItemCode == "Be_ShipTypeCN").FirstOrDefault(y => y.F_ItemValue == x.ShipTypeCN)?.F_ItemName;
                    x.ShipTypeENName = details.Where(y => y.F_ItemCode == "Be_ShipTypeEN").FirstOrDefault(y => y.F_ItemValue == x.ShipTypeEN)?.F_ItemName;
                    x.ShipSocietyName = details.Where(y => y.F_ItemCode == "Be_ShipSociety").FirstOrDefault(y => y.F_ItemValue == x.ShipSociety)?.F_ItemName;
                    x.FlagStateName = details.Where(y => y.F_ItemCode == "Be_FlagState").FirstOrDefault(y => y.F_ItemValue == x.FlagState)?.F_ItemName;
                });
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取项目选择页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<ec_projectEntity> GetProjectSelectPageList(Pagination pagination, string queryJson)
        {
            try
            {
                return ec_projectService.GetProjectSelectPageList(pagination, queryJson);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_projectEntity GetEntity(string keyValue)
        {
            try
            {
                return ec_projectService.GetEntity(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue,int projectIndex)
        {
            try
            {
                List<string> listTableCode = new List<string>();
                //从业务表中获取数据，复制表结构
                var listBusinessTable = new ec_business_tableBLL().GetList("{}").ToList();
                if (listBusinessTable != null && listBusinessTable.Count > 0)
                {
                    listTableCode = listBusinessTable.Select(x => x.BusinessTableCode).Distinct().ToList();
                }
                ec_projectService.DeleteEntity(keyValue, listTableCode, projectIndex);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_projectEntity entity)
        {
            try
            {
                ec_projectService.SaveEntity(keyValue, entity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 执行sql语句
        /// <param name="projectIndex">项目序号</param>
        /// <param name="listTableName">要复制的表名集合</param>
        /// <param name="isCopyData">是否要复制数据,默认为不复制</param>
        /// <param name="CopyTableData">新建项目时需要复制“是否复制数据”为1的表数据</param>
        /// <summary>
        /// <returns></returns>
        public void ExecuteSQL(List<string> listTableName, int projectIndex, bool isCopyData, List<string> CopyTableData)
        {
            try
            {
                ec_projectService.ExecuteSQL(listTableName, projectIndex, isCopyData, CopyTableData);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

    }
}
